package com.litxuf.util;

import java.util.List;

//import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.Criteria;

/**
 * 
 * @author XuFeng
 * 
 */
public class PageDiv<T> {
	private int curPage; // 当前页
	private int totalPage = -1; // 总页数
	private int totalRowCount; // 总记录数
	public int rowsPerPage = 2; // 每页记录数
	private List<T> list; //

	public PageDiv() {
		this.curPage = 1;
		this.totalRowCount = 0;
		this.rowsPerPage = 10;
	}

	public PageDiv(int curpage, int rowsPerPage) {
		this.curPage = curpage;
		this.rowsPerPage = rowsPerPage;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public List<T> getList() {
		return this.list;
	}

	public int getTotalRowCount() {
		return this.totalRowCount;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getCurPage() {
		return this.curPage;
	}

	public int getRowsPerPage() {
		return this.rowsPerPage;
	}

	public int getTotalPage() {

		return this.totalPage;
	}

	public boolean hasNext() {
		return this.getCurPage() < this.totalPage;
	}

	public boolean hasPre() {
		return this.getCurPage() > 1;
	}

	public void apply(Criteria criteria) {
		criteria.setMaxResults(rowsPerPage);
		criteria.setFirstResult((curPage - 1) * rowsPerPage);
	}

	public void setTotalRowCount(int totalRowCount) {
		this.totalRowCount = totalRowCount;
	}

	public void setRowsPerPage(int rowsPerPage) {
		this.rowsPerPage = rowsPerPage;
	}

	public void setTotalPage() {
		if (totalPage < 0)
			if (this.totalRowCount % this.rowsPerPage == 0) {
				this.totalPage = this.totalRowCount / this.rowsPerPage;
			} else {
				this.totalPage = this.totalRowCount / this.rowsPerPage + 1;
			}
	}

}
